<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: The Matrix class</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialMatrixClass.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">The Matrix class<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>In <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, all matrices and vectors are objects of the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> template class. Vectors are just a special case of matrices, with either 1 row or 1 column.</p>
<h1><a class="anchor" id="TutorialMatrixFirst3Params"></a>
The first three template parameters of Matrix</h1>
<p>The <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> class takes six template parameters, but for now it's enough to learn about the first three first parameters. The three remaining parameters have default values, which for now we will leave untouched, and which we <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixOptTemplParams">discuss below</a>.</p>
<p>The three mandatory template parameters of <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> are: </p><div class="fragment"><div class="line">Matrix&lt;typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime&gt;</div>
</div><!-- fragment --> <ul>
<li><code>Scalar</code> is the scalar type, i.e. the type of the coefficients. That is, if you want a matrix of floats, choose <code>float</code> here. See <a class="el" href="TopicScalarTypes.html">Scalar types</a> for a list of all supported scalar types and for how to extend support to new types. </li>
<li><code>RowsAtCompileTime</code> and <code>ColsAtCompileTime</code> are the number of rows and columns of the matrix as known at compile time (see <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixDynamic">below</a> for what to do if the number is not known at compile time).</li>
</ul>
<p>We offer a lot of convenience typedefs to cover the usual cases. For example, <code>Matrix4f</code> is a 4x4 matrix of floats. Here is how it is defined by <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>: </p><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;float, 4, 4&gt; <a class="code" href="group__matrixtypedefs.html#ga3a5de8dfef28d29aed525611e15a37e3">Matrix4f</a>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga3a5de8dfef28d29aed525611e15a37e3"><div class="ttname"><a href="group__matrixtypedefs.html#ga3a5de8dfef28d29aed525611e15a37e3">Eigen::Matrix4f</a></div><div class="ttdeci">Matrix&lt; float, 4, 4 &gt; Matrix4f</div><div class="ttdoc">4×4 matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment --><p> We discuss <a class="el" href="group__TutorialMatrixClass.html#TutorialMatrixTypedefs">below</a> these convenience typedefs.</p>
<h1><a class="anchor" id="TutorialMatrixVectors"></a>
Vectors</h1>
<p>As mentioned above, in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, vectors are just a special case of matrices, with either 1 row or 1 column. The case where they have 1 column is the most common; such vectors are called column-vectors, often abbreviated as just vectors. In the other case where they have 1 row, they are called row-vectors.</p>
<p>For example, the convenience typedef <code>Vector3f</code> is a (column) vector of 3 floats. It is defined as follows by <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>: </p><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;float, 3, 1&gt; <a class="code" href="group__matrixtypedefs.html#ga5ec9ce2d8adbcd6888f3fbf2e1c095a4">Vector3f</a>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga5ec9ce2d8adbcd6888f3fbf2e1c095a4"><div class="ttname"><a href="group__matrixtypedefs.html#ga5ec9ce2d8adbcd6888f3fbf2e1c095a4">Eigen::Vector3f</a></div><div class="ttdeci">Matrix&lt; float, 3, 1 &gt; Vector3f</div><div class="ttdoc">3×1 vector of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment --><p> We also offer convenience typedefs for row-vectors, for example: </p><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;int, 1, 2&gt; <a class="code" href="group__matrixtypedefs.html#ga9f54d6a47f5267f83c415ac54f5a89f3">RowVector2i</a>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga9f54d6a47f5267f83c415ac54f5a89f3"><div class="ttname"><a href="group__matrixtypedefs.html#ga9f54d6a47f5267f83c415ac54f5a89f3">Eigen::RowVector2i</a></div><div class="ttdeci">Matrix&lt; int, 1, 2 &gt; RowVector2i</div><div class="ttdoc">1×2 vector of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixDynamic"></a>
The special value Dynamic</h1>
<p>Of course, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> is not limited to matrices whose dimensions are known at compile time. The <code>RowsAtCompileTime</code> and <code>ColsAtCompileTime</code> template parameters can take the special value <code>Dynamic</code> which indicates that the size is unknown at compile time, so must be handled as a run-time variable. In <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> terminology, such a size is referred to as a <em>dynamic</em> <em>size</em>; while a size that is known at compile time is called a <em>fixed</em> <em>size</em>. For example, the convenience typedef <code>MatrixXd</code>, meaning a matrix of doubles with dynamic size, is defined as follows: </p><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;double, Dynamic, Dynamic&gt; <a class="code" href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">MatrixXd</a>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga99b41a69f0bf64eadb63a97f357ab412"><div class="ttname"><a href="group__matrixtypedefs.html#ga99b41a69f0bf64eadb63a97f357ab412">Eigen::MatrixXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, Dynamic &gt; MatrixXd</div><div class="ttdoc">Dynamic×Dynamic matrix of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --><p> And similarly, we define a self-explanatory typedef <code>VectorXi</code> as follows: </p><div class="fragment"><div class="line"><span class="keyword">typedef</span> Matrix&lt;int, Dynamic, 1&gt; <a class="code" href="group__matrixtypedefs.html#gaf20e523ca57ee8ef0a945cd4703d2bfd">VectorXi</a>;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaf20e523ca57ee8ef0a945cd4703d2bfd"><div class="ttname"><a href="group__matrixtypedefs.html#gaf20e523ca57ee8ef0a945cd4703d2bfd">Eigen::VectorXi</a></div><div class="ttdeci">Matrix&lt; int, Dynamic, 1 &gt; VectorXi</div><div class="ttdoc">Dynamic×1 vector of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment --><p> You can perfectly have e.g. a fixed number of rows with a dynamic number of columns, as in: </p><div class="fragment"><div class="line">Matrix&lt;float, 3, Dynamic&gt;</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixConstructors"></a>
Constructors</h1>
<p>A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients. You can do: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Matrix3f</a> a;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> b;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga276bae130c142e906ad8f47d24d11c1c"><div class="ttname"><a href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Eigen::Matrix3f</a></div><div class="ttdeci">Matrix&lt; float, 3, 3 &gt; Matrix3f</div><div class="ttdoc">3×3 matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga731599f782380312960376c43450eb48"><div class="ttname"><a href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">Eigen::MatrixXf</a></div><div class="ttdeci">Matrix&lt; float, Dynamic, Dynamic &gt; MatrixXf</div><div class="ttdoc">Dynamic×Dynamic matrix of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment --><p> Here, </p><ul>
<li><code>a</code> is a 3-by-3 matrix, with a plain float[9] array of uninitialized coefficients, </li>
<li><code>b</code> is a dynamic-size matrix whose size is currently 0-by-0, and whose array of coefficients hasn't yet been allocated at all.</li>
</ul>
<p>Constructors taking sizes are also available. For matrices, the number of rows is always passed first. For vectors, just pass the vector size. They allocate the array of coefficients with the given size, but don't initialize the coefficients themselves: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> a(10,15);</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga8028d921d43acd5605eabad41c254ef2">VectorXf</a> b(30);</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga8028d921d43acd5605eabad41c254ef2"><div class="ttname"><a href="group__matrixtypedefs.html#ga8028d921d43acd5605eabad41c254ef2">Eigen::VectorXf</a></div><div class="ttdeci">Matrix&lt; float, Dynamic, 1 &gt; VectorXf</div><div class="ttdoc">Dynamic×1 vector of type float.</div><div class="ttdef"><b>Definition:</b> Matrix.h:500</div></div>
</div><!-- fragment --><p> Here, </p><ul>
<li><code>a</code> is a 10x15 dynamic-size matrix, with allocated but currently uninitialized coefficients. </li>
<li><code>b</code> is a dynamic-size vector of size 30, with allocated but currently uninitialized coefficients.</li>
</ul>
<p>In order to offer a uniform API across fixed-size and dynamic-size matrices, it is legal to use these constructors on fixed-size matrices, even if passing the sizes is useless in this case. So this is legal: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Matrix3f</a> a(3,3);</div>
</div><!-- fragment --><p> and is a no-operation.</p>
<p>Matrices and vectors can also be initialized from lists of coefficients. Prior to C++11, this feature is limited to small fixed-size column or vectors up to size 4: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga6c206cbf6f8f3b74bc63ecd362fc2ad6">Vector2d</a> a(5.0, 6.0);</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gaabb0b4639dc0b48e691e02e95873b0f1">Vector3d</a> b(5.0, 6.0, 7.0);</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b">Vector4d</a> c(5.0, 6.0, 7.0, 8.0);</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga6c206cbf6f8f3b74bc63ecd362fc2ad6"><div class="ttname"><a href="group__matrixtypedefs.html#ga6c206cbf6f8f3b74bc63ecd362fc2ad6">Eigen::Vector2d</a></div><div class="ttdeci">Matrix&lt; double, 2, 1 &gt; Vector2d</div><div class="ttdoc">2×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga9b2fcb53776a2829871f8a49009bef0b"><div class="ttname"><a href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b">Eigen::Vector4d</a></div><div class="ttdeci">Matrix&lt; double, 4, 1 &gt; Vector4d</div><div class="ttdoc">4×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaabb0b4639dc0b48e691e02e95873b0f1"><div class="ttname"><a href="group__matrixtypedefs.html#gaabb0b4639dc0b48e691e02e95873b0f1">Eigen::Vector3d</a></div><div class="ttdeci">Matrix&lt; double, 3, 1 &gt; Vector3d</div><div class="ttdoc">3×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --><p>If C++11 is enabled, fixed-size column or row vectors of arbitrary size can be initialized by passing an arbitrary number of coefficients: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga3243031169188c73ed241df41cc979b6">Vector2i</a> a(1, 2);                      <span class="comment">// A column vector containing the elements {1, 2}</span></div>
<div class="line">Matrix&lt;int, 5, 1&gt; b {1, 2, 3, 4, 5};   <span class="comment">// A row-vector containing the elements {1, 2, 3, 4, 5}</span></div>
<div class="line">Matrix&lt;int, 1, 5&gt; c = {1, 2, 3, 4, 5}; <span class="comment">// A column vector containing the elements {1, 2, 3, 4, 5}</span></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga3243031169188c73ed241df41cc979b6"><div class="ttname"><a href="group__matrixtypedefs.html#ga3243031169188c73ed241df41cc979b6">Eigen::Vector2i</a></div><div class="ttdeci">Matrix&lt; int, 2, 1 &gt; Vector2i</div><div class="ttdoc">2×1 vector of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment --><p>In the general case of matrices and vectors with either fixed or runtime sizes, coefficients have to be grouped by rows and passed as an initializer list of initializer list (<a class="el" href="classEigen_1_1Matrix.html#a1b552edeee11ced34c3e471ad00b820a">details </a>): </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">MatrixXi</a> a {      <span class="comment">// construct a 2x2 matrix</span></div>
<div class="line">      {1, 2},     <span class="comment">// first row</span></div>
<div class="line">      {3, 4}      <span class="comment">// second row</span></div>
<div class="line">};</div>
<div class="line">Matrix&lt;double, 2, 3&gt; b {</div>
<div class="line">      {2, 3, 4},</div>
<div class="line">      {5, 6, 7},</div>
<div class="line">};</div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaceba5f63d4040c79c44109590d4fa606"><div class="ttname"><a href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">Eigen::MatrixXi</a></div><div class="ttdeci">Matrix&lt; int, Dynamic, Dynamic &gt; MatrixXi</div><div class="ttdoc">Dynamic×Dynamic matrix of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment --><p>For column or row vectors, implicit transposition is allowed. This means that a column vector can be initialized from a single row: </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">VectorXd</a> a {{1.5, 2.5, 3.5}};             <span class="comment">// A column-vector with 3 coefficients</span></div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">RowVectorXd</a> b {{1.0, 2.0, 3.0, 4.0}};     <span class="comment">// A row-vector with 4 coefficients</span></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga531c6af4be9297dbe53127a46c246e00"><div class="ttname"><a href="group__matrixtypedefs.html#ga531c6af4be9297dbe53127a46c246e00">Eigen::RowVectorXd</a></div><div class="ttdeci">Matrix&lt; double, 1, Dynamic &gt; RowVectorXd</div><div class="ttdoc">1×Dynamic vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga8554c6170729f01c7572574837ecf618"><div class="ttname"><a href="group__matrixtypedefs.html#ga8554c6170729f01c7572574837ecf618">Eigen::VectorXd</a></div><div class="ttdeci">Matrix&lt; double, Dynamic, 1 &gt; VectorXd</div><div class="ttdoc">Dynamic×1 vector of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialMatrixCoeffAccessors"></a>
Coefficient accessors</h1>
<p>The primary coefficient accessors and mutators in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> are the overloaded parenthesis operators. For matrices, the row index is always passed first. For vectors, just pass one index. The numbering starts at 0. This example is self-explanatory:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXd</a> m(2,2);</div>
<div class="line">  m(0,0) = 3;</div>
<div class="line">  m(1,0) = 2.5;</div>
<div class="line">  m(0,1) = -1;</div>
<div class="line">  m(1,1) = m(1,0) + m(0,1);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the matrix m:\n&quot;</span> &lt;&lt; m &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::VectorXd</a> v(2);</div>
<div class="line">  v(0) = 4;</div>
<div class="line">  v(1) = v(0) - 1;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the vector v:\n&quot;</span> &lt;&lt; v &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1Matrix_html"><div class="ttname"><a href="classEigen_1_1Matrix.html">Eigen::Matrix</a></div><div class="ttdoc">The matrix class, also used for vectors and row-vectors.</div><div class="ttdef"><b>Definition:</b> Matrix.h:182</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the matrix m:
  3  -1
2.5 1.5
Here is the vector v:
4
3
</pre> </td></tr>
</table>
<p>Note that the syntax <code>m(index)</code> is not restricted to vectors, it is also available for general matrices, meaning index-based access in the array of coefficients. This however depends on the matrix's storage order. All <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> matrices default to column-major storage order, but this can be changed to row-major, see <a class="el" href="group__TopicStorageOrders.html">Storage orders</a>.</p>
<p>The <code>operator[]</code> is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow <code>operator[]</code> to take more than one argument. We restrict <code>operator[]</code> to vectors, because an awkwardness in the C++ language would make <code>matrix[i,j]</code> compile to the same thing as <code>matrix[j]</code>!</p>
<h1><a class="anchor" id="TutorialMatrixCommaInitializer"></a>
Comma-initialization</h1>
<p>Matrix and vector coefficients can be conveniently set using the so-called <em>comma-initializer</em> syntax. For now, it is enough to know this example:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga276bae130c142e906ad8f47d24d11c1c">Matrix3f</a> m;</div>
<div class="line">m &lt;&lt; 1, 2, 3,</div>
<div class="line">     4, 5, 6,</div>
<div class="line">     7, 8, 9;</div>
<div class="line">std::cout &lt;&lt; m;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">1 2 3
4 5 6
7 8 9</pre>  </td></tr>
</table>
<p>The right-hand side can also contain matrix expressions as discussed in <a class="el" href="group__TutorialAdvancedInitialization.html">this page</a>.</p>
<h1><a class="anchor" id="TutorialMatrixSizesResizing"></a>
Resizing</h1>
<p>The current size of a matrix can be retrieved by <a class="el" href="structEigen_1_1EigenBase.html#ac22eb0695d00edd7d4a3b2d0a98b81c2">rows()</a>, <a class="el" href="structEigen_1_1EigenBase.html#a2d768a9877f5f69f49432d447b552bfe">cols() </a> and <a class="el" href="structEigen_1_1EigenBase.html#ae106171b6fefd3f7af108a8283de36c9">size()</a>. These methods return the number of rows, the number of columns and the number of coefficients, respectively. Resizing a dynamic-size matrix is done by the <a class="el" href="classEigen_1_1PlainObjectBase.html#a9fd0703bd7bfe89d6dc80e2ce87c312a">resize() </a> method.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXd</a> m(2,5);</div>
<div class="line">  m.resize(4,3);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The matrix m is of size &quot;</span></div>
<div class="line">            &lt;&lt; m.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; m.cols() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;It has &quot;</span> &lt;&lt; m.size() &lt;&lt; <span class="stringliteral">&quot; coefficients&quot;</span> &lt;&lt; std::endl;</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::VectorXd</a> v(2);</div>
<div class="line">  v.resize(5);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The vector v is of size &quot;</span> &lt;&lt; v.size() &lt;&lt; std::endl;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;As a matrix, v is of size &quot;</span></div>
<div class="line">            &lt;&lt; v.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; v.cols() &lt;&lt; std::endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">The matrix m is of size 4x3
It has 12 coefficients
The vector v is of size 5
As a matrix, v is of size 5x1
</pre>  </td></tr>
</table>
<p>The <code>resize()</code> method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change. If you want a conservative variant of <code>resize()</code> which does not change the coefficients, use <a class="el" href="classEigen_1_1PlainObjectBase.html#a712c25be1652e5a64a00f28c8ed11462">conservativeResize()</a>, see <a class="el" href="TopicResizing.html">this page</a> for more details.</p>
<p>All these methods are still available on fixed-size matrices, for the sake of API uniformity. Of course, you can't actually resize a fixed-size matrix. Trying to change a fixed size to an actually different value will trigger an assertion failure; but the following code is legal:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::Matrix4d</a> m;</div>
<div class="line">  m.<a class="code" href="classEigen_1_1PlainObjectBase.html#a9fd0703bd7bfe89d6dc80e2ce87c312a">resize</a>(4,4); <span class="comment">// no operation</span></div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;The matrix m is of size &quot;</span></div>
<div class="line">            &lt;&lt; m.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; m.cols() &lt;&lt; std::endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1PlainObjectBase_html_a9fd0703bd7bfe89d6dc80e2ce87c312a"><div class="ttname"><a href="classEigen_1_1PlainObjectBase.html#a9fd0703bd7bfe89d6dc80e2ce87c312a">Eigen::PlainObjectBase::resize</a></div><div class="ttdeci">void resize(Index rows, Index cols)</div><div class="ttdef"><b>Definition:</b> PlainObjectBase.h:283</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">The matrix m is of size 4x4
</pre>  </td></tr>
</table>
<h1><a class="anchor" id="TutorialMatrixAssignment"></a>
Assignment and resizing</h1>
<p>Assignment is the action of copying a matrix into another, using <code>operator=</code>. <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> resizes the matrix on the left-hand side automatically so that it matches the size of the matrix on the right-hand size. For example:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> a(2,2);</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;a is of size &quot;</span> &lt;&lt; a.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; a.cols() &lt;&lt; std::endl;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> b(3,3);</div>
<div class="line">a = b;</div>
<div class="line">std::cout &lt;&lt; <span class="stringliteral">&quot;a is now of size &quot;</span> &lt;&lt; a.rows() &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; a.cols() &lt;&lt; std::endl;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">a is of size 2x2
a is now of size 3x3
</pre>  </td></tr>
</table>
<p>Of course, if the left-hand side is of fixed size, resizing it is not allowed.</p>
<p>If you do not want this automatic resizing to happen (for example for debugging purposes), you can disable it, see <a class="el" href="TopicResizing.html">this page</a>.</p>
<h1><a class="anchor" id="TutorialMatrixFixedVsDynamic"></a>
Fixed vs. Dynamic size</h1>
<p>When should one use fixed sizes (e.g. <code>Matrix4f</code>), and when should one prefer dynamic sizes (e.g. <code>MatrixXf</code>)? The simple answer is: use fixed sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to. For small sizes, especially for sizes smaller than (roughly) 16, using fixed sizes is hugely beneficial to performance, as it allows <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> to avoid dynamic memory allocation and to unroll loops. Internally, a fixed-size <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> matrix is just a plain array, i.e. doing </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga3a5de8dfef28d29aed525611e15a37e3">Matrix4f</a> mymatrix; </div>
</div><!-- fragment --><p> really amounts to just doing </p><div class="fragment"><div class="line"><span class="keywordtype">float</span> mymatrix[16]; </div>
</div><!-- fragment --><p> so this really has zero runtime cost. By contrast, the array of a dynamic-size matrix is always allocated on the heap, so doing </p><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#ga731599f782380312960376c43450eb48">MatrixXf</a> mymatrix(rows,columns); </div>
</div><!-- fragment --><p> amounts to doing </p><div class="fragment"><div class="line"><span class="keywordtype">float</span> *mymatrix = <span class="keyword">new</span> <span class="keywordtype">float</span>[rows*columns]; </div>
</div><!-- fragment --><p> and in addition to that, the <code>MatrixXf</code> object stores its number of rows and columns as member variables.</p>
<p>The limitation of using fixed sizes, of course, is that this is only possible when you know the sizes at compile time. Also, for large enough sizes, say for sizes greater than (roughly) 32, the performance benefit of using fixed sizes becomes negligible. Worse, trying to create a very large matrix using fixed sizes inside a function could result in a stack overflow, since <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> will try to allocate the array automatically as a local variable, and this is normally done on the stack. Finally, depending on circumstances, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> can also be more aggressive trying to vectorize (use SIMD instructions) when dynamic sizes are used, see <a class="el" href="TopicVectorization.html">Vectorization</a>.</p>
<h1><a class="anchor" id="TutorialMatrixOptTemplParams"></a>
Optional template parameters</h1>
<p>We mentioned at the beginning of this page that the <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> class takes six template parameters, but so far we only discussed the first three. The remaining three parameters are optional. Here is the complete list of template parameters: </p><div class="fragment"><div class="line">Matrix&lt;<span class="keyword">typename</span> Scalar,</div>
<div class="line">       <span class="keywordtype">int</span> RowsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> ColsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> Options = 0,</div>
<div class="line">       <span class="keywordtype">int</span> MaxRowsAtCompileTime = RowsAtCompileTime,</div>
<div class="line">       <span class="keywordtype">int</span> MaxColsAtCompileTime = ColsAtCompileTime&gt;</div>
</div><!-- fragment --> <ul>
<li><code>Options</code> is a bit field. Here, we discuss only one bit: <code>RowMajor</code>. It specifies that the matrices of this type use row-major storage order; by default, the storage order is column-major. See the page on <a class="el" href="group__TopicStorageOrders.html">storage orders</a>. For example, this type means row-major 3x3 matrices: <div class="fragment"><div class="line">Matrix&lt;float, 3, 3, RowMajor&gt;</div>
</div><!-- fragment --> </li>
<li><code>MaxRowsAtCompileTime</code> and <code>MaxColsAtCompileTime</code> are useful when you want to specify that, even though the exact sizes of your matrices are not known at compile time, a fixed upper bound is known at compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation. For example the following matrix type uses a plain array of 12 floats, without dynamic memory allocation: <div class="fragment"><div class="line">Matrix&lt;float, Dynamic, Dynamic, 0, 3, 4&gt;</div>
</div><!-- fragment --></li>
</ul>
<h1><a class="anchor" id="TutorialMatrixTypedefs"></a>
Convenience typedefs</h1>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> defines the following <a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a> typedefs: </p><ul>
<li><code>MatrixNt</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;type, N, N&gt;</code>. For example, <code>MatrixXi</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;int, Dynamic, Dynamic&gt;</code>. </li>
<li><code>MatrixXNt</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;type, Dynamic, N&gt;</code>. For example, <code>MatrixX3i</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;int, Dynamic, 3&gt;</code>. </li>
<li><code>MatrixNXt</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;type, N, Dynamic&gt;</code>. For example, <code>Matrix4Xd</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;d, 4, Dynamic&gt;</code>. </li>
<li><code>VectorNt</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;type, N, 1&gt;</code>. For example, <code>Vector2f</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;float, 2, 1&gt;</code>. </li>
<li><code>RowVectorNt</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;type, 1, N&gt;</code>. For example, <code>RowVector3d</code> for <code><a class="el" href="classEigen_1_1Matrix.html" title="The matrix class, also used for vectors and row-vectors.">Matrix</a>&lt;double, 1, 3&gt;</code>.</li>
</ul>
<p>Where: </p><ul>
<li><code>N</code> can be any one of <code>2</code>, <code>3</code>, <code>4</code>, or <code>X</code> (meaning <code>Dynamic</code>). </li>
<li><code>t</code> can be any one of <code>i</code> (meaning <code>int</code>), <code>f</code> (meaning <code>float</code>), <code>d</code> (meaning <code>double</code>), <code>cf</code> (meaning <code>complex&lt;float&gt;</code>), or <code>cd</code> (meaning <code>complex&lt;double&gt;</code>). The fact that <code>typedef</code>s are only defined for these five types doesn't mean that they are the only supported scalar types. For example, all standard integer types are supported, see <a class="el" href="TopicScalarTypes.html">Scalar types</a>. </li>
</ul>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
